package com.alex.webnote.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.alex.common.tools.ImageUtils;
import com.alex.common.tools.MD5Utils;
import com.alex.common.tools.StringUtils;

@Entity
public class WebNotePO implements Serializable {

	private static final long serialVersionUID = -5149918548393085128L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@Column(length = 75, nullable = false, unique = true)
	private String alias;

	@Column(length = 20, nullable = false)
	private String username;

	@Column(length = 100, nullable = false)
	private String name;

	@Column(length = 300)
	private String tags;

	@Lob
	@Basic(fetch = FetchType.LAZY)
	@Column(columnDefinition = "LONGTEXT")
	private String noteContent;

	@Column(columnDefinition = "timestamp default now()", updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date createTime;

	@Temporal(TemporalType.TIMESTAMP)
	private Date updateTime;

	@Column(columnDefinition = "int default 0")
	private int state;

	@Column(columnDefinition = "int default 0")
	private int hots;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public String getNoteContent() {
		return noteContent;
	}

	public void setNoteContent(String noteContent) {
		this.noteContent = noteContent;
	}

	public String getUsername() {
		return username;
	}

	public String getAuthor_() {
		return MD5Utils.getMD5DigestHex(username);
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public int getHots() {
		return hots;
	}

	public void setHots(int hots) {
		this.hots = hots;
	}

	public String getSummary() {
		if (this.getNoteContent() != null && !this.getNoteContent().equals(""))
			return StringUtils.getLimitedCharString(this.getNoteContent(), 150)
					+ "……";
		return "";
	}

	public WebNoteSummary toSummary() {
		WebNoteSummary summary = new WebNoteSummary();
		summary.setId(this.getId());
		summary.setAlias(this.getAlias());
		List<String> images = ImageUtils.getImgStr(this.getNoteContent(), true);
		if (images != null && images.size() > 0) {
			summary.setImgurl(images.get(0));
		}
		summary.setSummary(getSummary());
		summary.setName(this.getName());
		summary.setHots(hots);
		return summary;
	}

}
